#include "StdAfx.h"
#include "TaskBuffer.h"
#include "FIFOBuffer.h"
#include "LIFOBuffer.h"
#include "RANDOMBuffer.h"


TaskBuffer::TaskBuffer( size_t size, BufferType * bufferType, Communicator & com):Actor(com)
{
	this->size = size;
	type = bufferType;
}


TaskBuffer::~TaskBuffer(void)
{

}


std::string TaskBuffer::toString()
{
	ostringstream str;
	str<< tasks.size() << "/" << size;
	return str.str();
}


bool TaskBuffer::addTask( Task * task )
{
	if(hasPlace()){
		tasks.push_back(task);
		return true;
	} else {
		return false;
	}
}


Task* TaskBuffer::popTask()
{	
	int taskID = (*type)(&tasks);
	Task * ret = *(tasks.begin()+taskID);
	tasks.erase(tasks.begin()+taskID);	
	return ret;
}


bool TaskBuffer::isEmpty()
{
	return tasks.empty();
}


bool TaskBuffer::hasPlace()
{
	return tasks.size()<size;
}


int TaskBuffer::getCount()
{
	return tasks.size();
}


int TaskBuffer::getCapacity()
{
	return size;
}

void TaskBuffer::tick()
{
	for(size_t i = 0; i<tasks.size(); i++){
		tasks[i]->incBuffer();
	}
}

